package cn.fl.project.service.impl;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysEmpBankAccApiService;
import cmm.user.facade.system.api.ISysUsrApiService;
import cmm.user.facade.system.condition.SysEmpBankAccQueryCondition;
import cmm.user.facade.system.condition.SysUsrQueryCondition;
import cmm.user.facade.system.dto.SysEmpBankAccDTO;
import cmm.user.facade.system.dto.SysUsrDTO;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.logger.eum.LogOperEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.service.IPrjLnkCompBkAccService;
import cn.fl.project.transform.PrjLnkCompBkAccVOTransform;
import cn.fl.project.vo.PrjLnkCompBkAccVO;
import fl.constant.facade.framework.constant.BusinessIdConstant;
import fl.flow.facade.flow.dto.FlowStartParamsDTO;
import fl.spl.facade.framework.constant.SplSystemConstant;
import fl.spl.facade.lnkcomp.api.IPrjLnkCompApiService;
import fl.spl.facade.lnkcomp.api.IPrjLnkCompQueryApiService;
import fl.spl.facade.lnkcomp.condition.PrjLnkCompBkAccQC;
import fl.spl.facade.lnkcomp.constant.LnkCompBankAccConstant;
import fl.spl.facade.lnkcomp.dto.PrjLnkCompBkAccDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 挂靠公司银行账户信息表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class PrjLnkCompBkAccServiceImpl implements IPrjLnkCompBkAccService {

	/**
	 * 本地数据字典服务
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	@Autowired
	private ISysUsrApiService sysUsrApiService;

	@Autowired
	private ISysEmpBankAccApiService sysEmpBankAccApiService;

	@Autowired
	private IPrjLnkCompQueryApiService prjLnkCompQueryApiService ;

	@Autowired
	private IPrjLnkCompApiService prjLnkCompApiService ;

	@Override
	public PrjLnkCompBkAccVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.newWebLogger("主键参数不能为空!").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SQOT_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			return null;
		}
        PrjLnkCompBkAccDTO dto = this.prjLnkCompQueryApiService.selectPrjLnkCompBkAccById(id, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
        return PrjLnkCompBkAccVOTransform.toVO(dto);
	}

	@Override
	public Long add(PrjLnkCompBkAccVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.newWebLogger("新增对象不能为空!").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SQOT_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			return null;
		}
		return this.prjLnkCompApiService.addPrjLnkCompBkAcc(PrjLnkCompBkAccVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			return false;
		}
		//return this.prjLnkCompBkAccApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}

	/**
     * 删除
     * @param ids
     * @return
     */
	@Override
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
			BaseLogUtils.newWebLogger("删除主键参数集合不能为空!").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SQOT_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			return false;
		};
		return this.prjLnkCompApiService.removePrjLnkCompBkAccByIdList(ids);
    }

	@Override
	public boolean modify(PrjLnkCompBkAccVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.newWebLogger("修改对象参数不能为空!").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SQOT_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			return false;
		}
		return this.prjLnkCompApiService.modifyPrjLnkCompBkAcc(PrjLnkCompBkAccVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page){
			page = new Pager();
		}
		page = this.prjLnkCompQueryApiService.searchPrjLnkCompBkAccListPage(page, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
		List<PrjLnkCompBkAccDTO> resultList = (List<PrjLnkCompBkAccDTO>) page.getResultList();
		List<PrjLnkCompBkAccVO> voList = PrjLnkCompBkAccVOTransform.toVOList(resultList);
		//设置创建人姓名
		SysUsrQueryCondition qc = new SysUsrQueryCondition();
		List<Long> ids = new ArrayList<Long>();
		if(CollectionUtils.isNotEmpty(voList)){
			for (PrjLnkCompBkAccVO prjLnkCompBkAccVO : voList) {
				if(prjLnkCompBkAccVO.getCrtUsrId()!= null){
					ids.add(prjLnkCompBkAccVO.getCrtUsrId());
				}
			}
		}
		qc.setUsrIds(ids);
		qc.setSysCd(CurrentThreadContext.getCurrentSysCd());
		List<SysUsrDTO> sysUsrList = this.sysUsrApiService.searchSysUsrList(qc);
		if( CollectionUtils.isNotEmpty(voList)&& CollectionUtils.isNotEmpty(sysUsrList)){
			for (PrjLnkCompBkAccVO prjLnkCompBkAccVO : voList) {
				for (SysUsrDTO sysUsrDTO : sysUsrList) {
					if(null!=prjLnkCompBkAccVO.getCrtUsrId() && prjLnkCompBkAccVO.getCrtUsrId().equals(sysUsrDTO.getId())){
						prjLnkCompBkAccVO.setCrtUsrNm(sysUsrDTO.getRefRlNm());
						break;
					}
				}
				SysEmpBankAccQueryCondition condition = new SysEmpBankAccQueryCondition();
				condition.setAccNo(prjLnkCompBkAccVO.getAccNo());
				List<SysEmpBankAccDTO> dtolist= this.sysEmpBankAccApiService.searchList(condition);
				if(CollectionUtils.isNotEmpty(dtolist)){
					if((dtolist.get(0).getIsDel())==0){
						prjLnkCompBkAccVO.setBdState("1");
					}else{
						prjLnkCompBkAccVO.setBdState("0");
					}
				}else{
					prjLnkCompBkAccVO.setBdState("0");
				}
			}
		}
		page.setResultList(voList);
		return page;
	}

	@Override
	public List<KeyValueVO> searchBizTypCdList() throws BaseException {
		return this.getKvList(LnkCompBankAccConstant.DIC_CODE_LNK_COMP_BIZ_TYP_CD);
	}

	@Override
	public List<KeyValueVO> searchCompPurpCdList() throws BaseException {
	    //只查询需要 isBankAcc 的账号用途
        return this.sysDictionaryApiService.getKvListWithAttr1(LnkCompBankAccConstant.DIC_CODE_LNK_COMP_PURP_CD, LnkCompBankAccConstant.DIC_CODE_LNK_COMP_PURP_CD_IS_BANK_ACC,
                CurrentThreadContext.getCurrentSysCd(),
                CurrentThreadContext.getCurrentOrgCd());
	}

	/**
	 * 根据字典中代码组值获取组员集合
	 * @param grpCd 代码组值
	 * @return 集合
	 */
	private List<KeyValueVO> getKvList(String grpCd){
		List<KeyValueVO> kvList = new ArrayList<>();
		try {
			List<SysDictionaryDTO> sysDictionaryDTOList = this.sysDictionaryApiService.searchGroup(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), grpCd);
			if(CollectionUtils.isNotEmpty(sysDictionaryDTOList)){
				for (SysDictionaryDTO sysDictionaryDTO : sysDictionaryDTOList) {
					KeyValueVO keyValueVO = new KeyValueVO();
					keyValueVO.setKey(sysDictionaryDTO.getCode());
					keyValueVO.setValue(sysDictionaryDTO.getCodeNm());
					kvList.add(keyValueVO);
				}
			}
		} catch (BaseException e) {
			BaseLogUtils.newWebLogger("根据字典中代码组值获取组员集合,系统异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SQOT_MANAGER_CD)
					.setWarning(false)
					.setThrowable(e)
					.warn(LogExceptionEnum.COMMON_CODE_0001);
		} catch (Exception e) {
			BaseLogUtils.newWebLogger("根据字典中代码组值获取组员集合,系统异常").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SQOT_MANAGER_CD)
					.setWarning(false)
					.setThrowable(e)
					.error(LogExceptionEnum.COMMON_CODE_0099);
		}
		return kvList;
	}

	@Override
	public List<PrjLnkCompBkAccVO> getPrjLnkCompBkAccCondition(PrjLnkCompBkAccQC qc) throws BaseException {
		return PrjLnkCompBkAccVOTransform.toVOList(this.prjLnkCompQueryApiService.getPrjLnkCompBkAccByCondition(qc));

	}

	@Override
	public DataResultDTO addFlowStart(FlowStartParamsDTO flowStartParamsDTO)
			throws BaseException {
		if(StringUtils.isEmpty(flowStartParamsDTO.getCrtUsrId()) || StringUtils.isEmpty(flowStartParamsDTO.getBusinessKey())){
			BaseLogUtils.newWebLogger("用户ID和业务数据主键必须要存在!").setLogOperType(LogOperEnum.REQ)
					.setSysCd(CurrentThreadContext.getCurrentSysCd())
					.setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
					.setBussinessId(BusinessIdConstant.LOG_SQOT_MANAGER_CD)
					.setWarning(false)
					.warn(LogExceptionEnum.COMMON_CODE_0002);
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "用户ID和业务数据主键必须要存在!");
		}
		return this.prjLnkCompApiService.addLnkCompFlowStart(flowStartParamsDTO);
	}
}
